<?php
include_once("inc/conn.php");
include_once("inc/function.php");
include_once("heheadmin/inc/config.php");
include_once ("class/Sms.php");

/*
if($_COOKIE["reg"]==1)
{
    echo "每个人只允许注册一个帐号，请勿重复注册!";
    exit;
} */
$vcode = $_POST["vcode"];
if($vcode != $_SESSION["CheckNum"])
{
    //echo "验证码错误!";
    //exit;
}
if($_REQUEST['code']=='' || $_SESSION['mobilesmscode']!=$_REQUEST['code']){
    //die('短信验证码错误');
}

$username = str_check($_POST["username"]);
$pass = $_POST["pass"];
$nickname = str_check($_POST["nickname"]);
$tjid = str_check($_POST["tjid"]);
$nickname = str_replace("http","",$nickname);
$nickname = str_replace("href","",$nickname);

if(!preg_match("/^1[34578]{1}\d{9}$/",$username)){
    die('请输入正确的手机号码');
}
if(strlen($username) != 11)
{
    die('请输入正确的手机号码');
    exit;
}
$Sms=new Sms();
$code=$Sms->get_code($username);
if($_REQUEST['vcode']=='' || $code!=$_REQUEST['vcode']){
    die("手机跟验证码不匹配");
}
if($nickname == "" || strlen($nickname) > 20)
{
    echo "昵称错误,长度不超过20位!";
    exit;
}
if(strlen($pass) == 0 || strlen($pass) > 30)
{
    echo "密码错误,长度不超过30位!";
    exit;
}
if(  strlen($tjid) > 20)
{
    echo "推荐人ID错误,长度不超过20位!";
    exit;
}
if($tjid==""){
    $tjid=0;
}
$tjid = intval($tjid );//转换成整形
if($tjid<0){
    $tjid =0;
}

$pass = md5($web_pwd_encrypt_prefix . $pass);
$arrRet = array('cmd'=>'');
$ip = usersip();
//$enUserName = ChangeEncode($username);
//$enNickName = ChangeEncode($nickname);
$sql = "call web_user_mobile_reg(0,0,'{$username}','{$nickname}','{$pass}','{$ip}',{$tjid})";
$arr = $db->Mysqli_Multi_Query($sql);
switch($arr[0][0]["result"])
{
    case '0': //成功
        $_SESSION["usersid"] = $arr[0][0]["userid"];
        $_SESSION["username"] = $arr[0][0]["username"];
        $_SESSION["password"] = $pass;
        $_SESSION["nickname"] = $arr[0][0]["nickname"];
        $_SESSION["points"] = $arr[0][0]["points"];
        $_SESSION["bankpoints"] = 0;
        $_SESSION["exp"] = $arr[0][0]["experience"];
        setcookie("usersid",$arr[0][0]["userid"]);
        setcookie("username",$arr[0][0]["username"]);
        setcookie("password",$pwd);
        setcookie("reg",1,time()+8640);
        $arrRet['cmd'] = 'ok';
        break;
    case '1': //用户名重名
        $arrRet['cmd'] = '很抱歉！帐号重名了，请更改！';
        break;
    case '2': //
        $arrRet['cmd'] = '很抱歉!用户名含有不允许字符，请更改!';
        break;
    case '3': //
        $arrRet['cmd'] = '很抱歉!用户数字ID已存在，请重新注册!';
        break;
    case '4': //
        $arrRet['cmd'] = '很抱歉!昵称重名，请更改！';
        break;
    case '99': //数据库错误
        $arrRet['cmd'] = '很抱歉！系统错误，请与客服联系！';
        break;
    default:
        $arrRet['cmd'] = '未知错误';
        break;
}
echo $arrRet['cmd'];
exit;